import { ModsInstance } from "@/func/createMod";
import { BaseIO } from "./baseIO";
import { getModeFuncRet, RetResult } from "../tools/modHelper";

// 媒介状态
type MediaStatus =
	| "PRESENT"
	| "NOTPRESENT"
	| "JAMMED"
	| "NOTSUPP"
	| "UNKNOWN"
	| "ENTERING"
	| "RETRACTED";

// 打印机状态
type DeviceStatus =
	| "HEALTHY"
	| "HWERROR"
	| "NODEVICE"
	| "OFFLINE"
	| "POWEROFF"
	| "USERERROR"
	| "BUSY"
	| "FRAUDATTEMPT"
	| "POTENTIALFRAUD";

// 纸供给状态
type PaperStatus = "FULL" | "LOW" | "OUT" | "NOTSUPP" | "UNKNOWN" | "JAMMED";

// 打印机中盖章墨水的状态（如果可能）
type InkStatus = "InkStatus" | "FULL" | "LOW" | "OUT" | "NOTSUPP";

export interface A4State {
	StMediaStatus: MediaStatus;
	StDeviceStatus: DeviceStatus;
	StPaperStatus: PaperStatus;
	StInkStatus: InkStatus;
}
export interface A4Capability { }

export type PrintRet = "PrintComplete" | "DeviceError" | "FatalError";

export interface ExtendedPrintParams {
	FormName: string;
	FieldValues: string;
	MediaName: string;
	Alignment: string;
	XOffset: number;
	YOffset: number;
	MediaAction: string;
}

export class A4 extends BaseIO<A4State, A4Capability> {
	constructor(modName: keyof ModsInstance, mod: any) {
		super(modName, mod);
	}

	ExtendedPrint(Params: ExtendedPrintParams) {
		Trace(EC_INFO, `-->Prr:ExtendedPrint`);
		this.setCurrentMethod("ExtendedPrint");
		this.removeMethodBindEvt("ExtendedPrint");
		return new Promise<RetResult<PrintRet>>((resolve, reject) => {
			const PrintComplete = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ExtendedPrint PrintComplete`);
				resolve(getModeFuncRet(this.modName, "PrintComplete", args));
			};
			const DeviceError = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ExtendedPrint DeviceError`);
				resolve(getModeFuncRet(this.modName, "DeviceError", args));
			};
			const FatalError = (...args: any[]) => {
				Trace(EC_INFO, `<--${this.modName}:ExtendedPrint FatalError`);
				resolve(getModeFuncRet(this.modName, "FatalError", args));
			};

			this.add("PrintComplete", PrintComplete);
			this.add("DeviceError", DeviceError);
			this.add("FatalError", FatalError);

			this.mod.ExtendedPrint(
				Params.FormName,
				Params.FieldValues,
				Params.MediaName,
				Params.Alignment,
				Params.XOffset,
				Params.YOffset,
				Params.MediaAction,
				(ret: string) => {
					if (parseInt(ret) != 0) {
						Trace(EC_INFO, `<--${this.modName}:ExtendedPrint ret=` + ret);
						resolve(getModeFuncRet(this.modName, "FatalError"));
					}
				}
			);
		}).then((value) => {
			this.removeMethodBindEvt("ExtendedPrint");
			return value;
		});
	}
}
